﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WGM_Silverlight.Controls;
using System.Windows.Threading;
using WGM_Silverlight.Classes;
using System.Windows.Controls.Primitives;
using System.Windows.Browser;
using System.Windows.Media.Imaging;

namespace WGM_Silverlight.Views
{
    public partial class MainView : UserControl
    {
        private string _networkName;
        private string _networkPassword;
        private string _username;
        private string _password;
        private string _macAddress;
        WGM_Svc.ServiceSoapClient _wgmSvc = null;
        DispatcherTimer _retrieveMessagesTimer = new DispatcherTimer();
        DispatcherTimer _refreshContactsTimer = new DispatcherTimer();
        DispatcherTimer _unreadTimer = new DispatcherTimer();
        private bool _retrieveInProgress = false;
        public ContactInfo MyContactInfo { get; set; }
        public DateTime LogonTime { get; set; }
        int _blinkCount = 0;
        List<string> _allFonts;
        public MainView()
        {
            InitializeComponent();
            _retrieveMessagesTimer.Interval = TimeSpan.FromSeconds(2);
            _refreshContactsTimer.Interval = TimeSpan.FromMinutes(10);
            _unreadTimer.Interval = TimeSpan.FromMilliseconds(500);
            _unreadTimer.Tick += new EventHandler(_unreadTimer_Tick);
            _retrieveMessagesTimer.Tick += new EventHandler(_retrieveMessagesTimer_Tick);
            _refreshContactsTimer.Tick += new EventHandler(_refreshContactsTimer_Tick);
            _contactScroller.SetIsMouseWheelScrollingEnabled(true);
            _chatControl.SelectionChanged += new SelectionChangedEventHandler(_chatControl_SelectionChanged);

        }

        void _unreadTimer_Tick(object sender, EventArgs e)
        {
            _blinkCount++;
            if (_blinkCount > 5)
            {
                _blinkCount = 0;
                _unreadTimer.Stop();
                HtmlPage.Window.Eval(string.Format("document.title=\"{0}\"", "WGM Beta"));
                CheckForUnreadMessages();
                return;
            }
            string title = "New Message";
            for (int count = 0; count < _blinkCount; count++)
            {
                title = "*" + title;
            }
            HtmlPage.Window.Eval(string.Format("document.title=\"{0}\"", title));
        }

        void _chatControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            foreach (var tab in e.RemovedItems)
            {
                ContactInfo ci = (tab as TabItem).Tag as ContactInfo;
                ci.IsSelected = false;
            }
            
        }

        void _refreshContactsTimer_Tick(object sender, EventArgs e)
        {
            _wgmSvc.GetAllUserOptionsAsync(_username, _password, _networkName);
        }

        void _retrieveMessagesTimer_Tick(object sender, EventArgs e)
        {
            if (!_retrieveInProgress)
            {
                _retrieveInProgress = true;
                _wgmSvc.RetrieveMessagesAsync(_username, _password, _networkName, _macAddress);
            }
        }
        public void SetInformation(WGM_Svc.ServiceSoapClient wgmSvc,string username, string password, string networkName, string networkPassword, string macAddress)
        {
            _username = username;
            _password = password;
            _macAddress = macAddress;
            _networkPassword = networkPassword;
            _wgmSvc = wgmSvc;
            _networkName = networkName;
            InitWebService();
        }

        private void InitWebService()
        {
            _wgmSvc.RetrieveMessagesCompleted   +=new EventHandler<WGM_Silverlight.WGM_Svc.RetrieveMessagesCompletedEventArgs>(_wgmSvc_RetrieveMessagesCompleted);
            _wgmSvc.SendMessageCompleted        += new EventHandler<WGM_Silverlight.WGM_Svc.SendMessageCompletedEventArgs>(_wgmSvc_SendMessageCompleted);
            _wgmSvc.GetAllUserNamesCompleted    += new EventHandler<WGM_Silverlight.WGM_Svc.GetAllUserNamesCompletedEventArgs>(_wgmSvc_GetAllUserNamesCompleted);
            _wgmSvc.GetOnlineUserNamesCompleted += new EventHandler<WGM_Silverlight.WGM_Svc.GetOnlineUserNamesCompletedEventArgs>(_wgmSvc_GetOnlineUserNamesCompleted);
            _wgmSvc.GetServerTimeCompleted      += new EventHandler<WGM_Silverlight.WGM_Svc.GetServerTimeCompletedEventArgs>(_wgmSvc_GetServerTimeCompleted);
            _wgmSvc.GetAllUserOptionsCompleted  += new EventHandler<WGM_Silverlight.WGM_Svc.GetAllUserOptionsCompletedEventArgs>(_wgmSvc_GetAllUserOptionsCompleted);
            _wgmSvc.GetUserImageCompleted       += new EventHandler<WGM_Silverlight.WGM_Svc.GetUserImageCompletedEventArgs>(_wgmSvc_GetUserImageCompleted);
            _wgmSvc.GetUserOptionsCompleted     += new EventHandler<WGM_Silverlight.WGM_Svc.GetUserOptionsCompletedEventArgs>(_wgmSvc_GetUserOptionsCompleted);
            _wgmSvc.GetAllFontsCompleted        += new EventHandler<WGM_Silverlight.WGM_Svc.GetAllFontsCompletedEventArgs>(_wgmSvc_GetAllFontsCompleted);
            _wgmSvc.SetUserImageCompleted += new EventHandler<WGM_Silverlight.WGM_Svc.SetUserImageCompletedEventArgs>(_wgmSvc_SetUserImageCompleted);
        }

        void _wgmSvc_SetUserImageCompleted(object sender, WGM_Silverlight.WGM_Svc.SetUserImageCompletedEventArgs e)
        {
            
        }

        void _wgmSvc_GetAllFontsCompleted(object sender, WGM_Silverlight.WGM_Svc.GetAllFontsCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                _allFonts = e.Result.ToList();
            }
        }

        void _wgmSvc_GetUserOptionsCompleted(object sender, WGM_Silverlight.WGM_Svc.GetUserOptionsCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                if (e.Result != null)
                {
                    bool newUser = (bool)e.UserState;
                    if (!newUser)
                    {
                        if(HelperFunctions.AreStringsEqual(e.Result.UserName,_username,true))
                        {
                            ContactInfo ci = new ContactInfo(e.Result,this);
                            SetMyContactInfo(ci);
                        }
                        else
                        {
                            ContactInfo ci = FindContactInfo(false, e.Result.UserName);
                            TabItem ti = FindChatControl(false, e.Result.UserName);
                            ChatControl cc = ti.Content as ChatControl;
                            if (cc != null && ci != null)
                            {
                                ci.SetUserOptions(e.Result);
                                cc.SetContactInfo(ci);
                            }
                        }
                    }
                    else
                    {
                        ContactInfo newCi = new ContactInfo(e.Result,this);
                        TabItem ti = new TabItem();
                        ChatControl cc = new ChatControl(newCi.ContactUsername, this);
                        cc.SetContactInfo(newCi);
                        cc.SetOutgoingTextOptions(MyContactInfo);
                        ti.Content = cc;
                        newCi.Tag = ti;
                        ti.Tag = newCi;
                        newCi.MouseLeftButtonDown+=new MouseButtonEventHandler(ci_MouseLeftButtonDown);
                        _contactList.Children.Add(newCi);                        
                    }
                    List<ContactInfo> contacts = new List<ContactInfo>();
                    ContactInfo mainWindow = null;
                    foreach (ContactInfo ci in _contactList.Children)
                    {
                        if (!ci.IsMainWindow)
                            contacts.Add(ci);
                        else
                            mainWindow = ci;
                    }
                    _contactList.Children.Clear();
                    contacts = (from contact in contacts
                                orderby contact.ContactDisplayName ascending
                                select contact).ToList();
                    _contactList.Children.Add(mainWindow);
                    foreach (var contact in contacts)
                        _contactList.Children.Add(contact);
                    
                }
            }
        }

        void _wgmSvc_GetUserImageCompleted(object sender, WGM_Silverlight.WGM_Svc.GetUserImageCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                string user = e.UserState as string;
                if (HelperFunctions.AreStringsEqual(user, _username, true))
                {
                    if (e.Result != null)
                    {
                        _myContactImageBrush.ImageSource = HelperFunctions.ByteArrayToImage(e.Result);
                    }
                    else
                        _myContactImageBrush.ImageSource = new BitmapImage(new Uri("../Images/blank.png", UriKind.RelativeOrAbsolute));
                }
                else
                {
                    ChatControl cc = FindChatControl(false, user).Content as ChatControl;
                    if (cc != null)
                    {
                        if (e.Result != null)
                            cc.SetContactImage(HelperFunctions.ByteArrayToImage(e.Result));
                        else
                            cc.ClearContactImage();
                    }
                }
            }
        }

        void _wgmSvc_GetAllUserOptionsCompleted(object sender, WGM_Silverlight.WGM_Svc.GetAllUserOptionsCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                if (e.Result != null)
                {
                    _progressMessage.Text = "Contact Retrievel Completed...";
                    _retyPanel.Visibility = Visibility.Collapsed;
                    _progressPanel.Visibility = Visibility.Collapsed;

                    ContactInfo mainWindow = new ContactInfo(null,this);
                    TabItem mainItem = new TabItem();
                    mainItem.Visibility = Visibility.Collapsed;
                    ChatControl main = new ChatControl(mainWindow.ContactUsername, this) { IsMainWindow = true };
                    main.SetContactInfo(mainWindow);
                    mainItem.Content = main;
                    mainItem.Tag = mainWindow;
                    mainWindow.Tag = mainItem;
                    mainWindow.MouseLeftButtonDown += new MouseButtonEventHandler(ci_MouseLeftButtonDown);
                    _contactList.Children.Add(mainWindow);
                    _chatControl.Items.Add(mainItem);


                    var allContactOptions = (from co in e.Result
                                             orderby co.DisplayName ascending
                                             select co).ToList();
                    foreach (var contactOptions in allContactOptions)
                    {
                        ContactInfo ci = new ContactInfo(contactOptions,this);
                        if (HelperFunctions.AreStringsEqual(ci.ContactUsername, _username, true))
                        {
                            SetMyContactInfo(ci);
                            _wgmSvc.GetUserImageAsync(_username, _username, _password, _username);
                            continue;
                        }
                        TabItem newItem = new TabItem();
                        newItem.Visibility = Visibility.Collapsed;
                        ChatControl cc = new ChatControl(ci.ContactUsername, this);
                        cc.SetContactInfo(ci);
                        _wgmSvc.GetUserImageAsync(ci.ContactUsername, _username, _password, ci.ContactUsername);
                        newItem.Content = cc;
                        newItem.Tag = ci;
                        ci.Tag = newItem;
                        ci.MouseLeftButtonDown += new MouseButtonEventHandler(ci_MouseLeftButtonDown);
                        _contactList.Children.Add(ci);
                        _chatControl.Items.Add(newItem);                        
                    }
                    SetMyContactInfo(MyContactInfo);
                    _retrieveMessagesTimer.Start();
                    ci_MouseLeftButtonDown(_contactList.Children.First(), null);
                    _contactScroller.UpdateLayout();
                    _wgmSvc.GetOnlineUserNamesAsync(_networkName);
                }
                else
                {
                    _progressMessage.Text = e.statusMessage;
                    _retyPanel.Visibility = Visibility.Visible;
                }
            }
            else
            {
                _progressMessage.Text = "There was an error retrieving the contact list...";
                _retyPanel.Visibility = Visibility.Visible;
            }
        }
        private void SetMyContactInfo(ContactInfo ci)
        {
            MyContactInfo = ci;
            _myContactNameTb.Text = ci.ContactDisplayName;
            _myContactStatusTb.Text = ci.ContactDisplayStatus;
            foreach (TabItem ti in _chatControl.Items)
            {
                ChatControl cc = ti.Content as ChatControl;
                cc.SetOutgoingTextOptions(MyContactInfo);
            }
        }

        void ci_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            ContactInfo ci = sender as ContactInfo;
            TabItem tab = ci.Tag as TabItem;
            ci.IsSelected = true;
            tab.IsSelected = true;
            CheckForUnreadMessages();
        }

        void _wgmSvc_GetServerTimeCompleted(object sender, WGM_Silverlight.WGM_Svc.GetServerTimeCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                LogonTime = e.Result;
            }
        }

        void _wgmSvc_GetOnlineUserNamesCompleted(object sender, WGM_Silverlight.WGM_Svc.GetOnlineUserNamesCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                if (e.Result != null)
                {
                    foreach(ContactInfo ci in _contactList.Children)
                    {
                        if (e.Result.Contains(ci.ContactUsername))
                            ci.IsOnline = true;
                    }                                   
                }
            }
        }

        void _wgmSvc_GetAllUserNamesCompleted(object sender, WGM_Silverlight.WGM_Svc.GetAllUserNamesCompletedEventArgs e)
        {
            throw new NotImplementedException();
        }

        void _wgmSvc_SendMessageCompleted(object sender, WGM_Silverlight.WGM_Svc.SendMessageCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                object[] parms = e.UserState as object[];
                ChatControl cc = parms[1] as ChatControl;
                WGM_Svc.WGMMessage message = parms[0] as WGM_Svc.WGMMessage;
                cc.RecieveMessage(MyContactInfo, message, true);
            }
        }

        void _wgmSvc_RetrieveMessagesCompleted(object sender, WGM_Silverlight.WGM_Svc.RetrieveMessagesCompletedEventArgs e)
        {
            _retrieveInProgress = false;
            if (e.Error == null)
            {
                _statusRow.Height = new GridLength(0);
                if (e.Result == true)
                {
                    foreach (var message in e.messages)
                    {
                        switch (message.MessageType)
                        {
                            case WGM_Silverlight.WGM_Svc.MessageTypes.GLOBAL_MESSAGE:
                            case WGM_Silverlight.WGM_Svc.MessageTypes.PRIVATE_MESSAGE:
                                ReceiveTextMessage(message);
                                break;
                            case WGM_Svc.MessageTypes.USER_LOGON:
                                if (HelperFunctions.IsMessageValid(message, LogonTime))
                                {
                                    ReceiveUserLogonMessage(message);
                                }
                                break;
                            case WGM_Svc.MessageTypes.USER_LEAVING:
                                if (HelperFunctions.IsMessageValid(message, LogonTime))
                                {
                                    ReceiveUserLogoffMessage(message);
                                }
                                break;
                            case WGM_Svc.MessageTypes.USER_CHANGED_INFO:
                                if (HelperFunctions.IsMessageValid(message, LogonTime))
                                {
                                    _wgmSvc.GetUserOptionsAsync(message.SenderLogon, _username, _password, false);
                                }
                                break;
                            case WGM_Svc.MessageTypes.USER_CHANGED_IMAGE:
                                if (HelperFunctions.IsMessageValid(message, LogonTime))
                                {
                                    _wgmSvc.GetUserImageAsync(message.SenderLogon, _username, _password, message.SenderLogon);

                                }
                                break;
                            case WGM_Svc.MessageTypes.USER_JOINED_NETWORK:
                                if (HelperFunctions.IsMessageValid(message, LogonTime))
                                {
                                    _wgmSvc.GetUserOptionsAsync(message.SenderLogon, _username, _password, true);
                                }
                                break;
                            default:
                                return;
                        }
                    }
                }
                else
                {
                    _statusMessage.Text = "Reconnecting...";
                    _wgmSvc.JoinNetworkAsync(_username, _password, _networkName, _networkPassword, _macAddress);
                }
            }
            else
            {
                _statusMessage.Text = "You are currently experiencing connection issues...";
                _statusRow.Height = new GridLength(15);
            }
        }

        private void ReceiveUserLogoffMessage(WGM_Silverlight.WGM_Svc.WGMMessage message)
        {
            ContactInfo ci = FindContactInfo(false, message.SenderLogon);
            if (ci != null)
                ci.IsOnline = false;
        }

        private void ReceiveUserLogonMessage(WGM_Silverlight.WGM_Svc.WGMMessage message)
        {
            ContactInfo ci = FindContactInfo(false, message.SenderLogon);
            if (ci != null)
                ci.IsOnline = true;
        }

        private void ReceiveTextMessage(WGM_Silverlight.WGM_Svc.WGMMessage message)
        {
            ContactInfo senderInfo = null;
            TabItem destinationControl = null;
            if (!HelperFunctions.AreStringsEqual(message.SenderLogon, _username, true))
            {
                senderInfo = FindContactInfo(false, message.SenderLogon);
            }
            else
            {
                senderInfo = MyContactInfo;
            }

            if (message.MessageType == WGM_Silverlight.WGM_Svc.MessageTypes.GLOBAL_MESSAGE)
            {                
                destinationControl = FindChatControl(true, "");
            }
            else
            {
                if (!HelperFunctions.AreStringsEqual(message.SenderLogon, _username, true))
                {                    
                    destinationControl = FindChatControl(false, message.SenderLogon);
                }
                else
                {
                    destinationControl = FindChatControl(false, message.ReceiverLogon);
                }
            }
            if (destinationControl != null && senderInfo != null)
            {
                (destinationControl.Content as ChatControl).RecieveMessage(senderInfo, message,false);
                ContactInfo associatedContact = destinationControl.Tag as ContactInfo;
                associatedContact.HasNewMessage = true;
                _contactScroller.UpdateLayout();
                if(!associatedContact.IsSelected)
                    _contactScroller.ScrollIntoView(associatedContact);

                CheckForUnreadMessages();
            }
            if(!_unreadTimer.IsEnabled)
                _unreadTimer.Start();
        }

        private bool CheckForUnreadMessages()
        {
            foreach (ContactInfo ci in _contactList.Children)
            {
                if (ci.HasNewMessage)
                {
                    _unreadMessagesImage.Visibility = Visibility.Visible;
                    if(!_unreadTimer.IsEnabled)
                        HtmlPage.Window.Eval(string.Format("document.title=\"{0}\"", "New Message(s) waiting"));
                    return true;
                }
            }
            if (!_unreadTimer.IsEnabled)
                HtmlPage.Window.Eval(string.Format("document.title=\"{0}\"", "WGM Beta"));

            _unreadMessagesImage.Visibility = Visibility.Collapsed;
            return false;
        }

        private TabItem FindChatControl(bool isMainWindow, string username)
        {
            TabItem retVal = null;

            foreach (TabItem tabItem in _chatControl.Items)
            {
                var cc = tabItem.Content as ChatControl;

                if (isMainWindow && cc.IsMainWindow)
                {
                    retVal = tabItem;
                    break;
                }
                else if (HelperFunctions.AreStringsEqual(username, cc.Username, true))
                {
                    retVal = tabItem;
                    break;
                }

            }


            return retVal;
        }

        private ContactInfo FindContactInfo(bool isMainWindow, string username)
        {
            ContactInfo retVal = null;
            foreach (var child in _contactList.Children)
            {
                var ci = child as ContactInfo;
                if (isMainWindow && ci.IsMainWindow)
                {
                    retVal = ci;
                    break;
                }
                else if (HelperFunctions.AreStringsEqual(username, ci.ContactUsername, true))
                {
                    retVal = ci;
                    break;
                }

            }

            return retVal;
        }
        public event MouseButtonEventHandler BackToLobbyClick
        {
            add
            {
                _backToLobbyBtn.MouseLeftButtonDown += value;
            }
            remove
            {
                _backToLobbyBtn.MouseLeftButtonDown -= value;
            }
        }
        public event MouseButtonEventHandler OptionsClick
        {
            add
            {
                _optionsBtn.MouseLeftButtonDown += value;
            }
            remove
            {
                _optionsBtn.MouseLeftButtonDown -= value;
            }
        }
        public string NetworkName
        {
            get { return _currentNetworkTB.Text; }
            set { _currentNetworkTB.Text = value; }
        }
        public string NetworkDescription
        {
            get { return _currentNetworkDescTB.Text; }
            set { _currentNetworkDescTB.Text = value; }
        }

        internal void ShutDown()
        {
            _wgmSvc.LogOffAsync(_username, _password, _macAddress, _networkName);
            _chatControl.Items.Clear();
            _contactList.Children.Clear();
            _retrieveMessagesTimer.Stop();
            _refreshContactsTimer.Stop();
            _currentNetworkDescTB.Text = "";
            _currentNetworkTB.Text = "";
            _chatTab.IsSelected = true;
            _unreadTimer.Stop();
            HtmlPage.Window.Eval(string.Format("document.title=\"{0}\"", "WGM Beta"));
            _wgmSvc.RetrieveMessagesCompleted   -= _wgmSvc_RetrieveMessagesCompleted;
            _wgmSvc.SendMessageCompleted        -= _wgmSvc_SendMessageCompleted;
            _wgmSvc.GetAllUserNamesCompleted    -= _wgmSvc_GetAllUserNamesCompleted;
            _wgmSvc.GetOnlineUserNamesCompleted -= _wgmSvc_GetOnlineUserNamesCompleted;
            _wgmSvc.GetServerTimeCompleted      -= _wgmSvc_GetServerTimeCompleted;
            _wgmSvc.GetAllUserOptionsCompleted  -= _wgmSvc_GetAllUserOptionsCompleted;
            _wgmSvc.GetUserImageCompleted       -= _wgmSvc_GetUserImageCompleted;
            _wgmSvc.GetUserOptionsCompleted     -= _wgmSvc_GetUserOptionsCompleted;
            _wgmSvc.GetAllFontsCompleted        -= _wgmSvc_GetAllFontsCompleted;

        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            LogonTime = DateTime.Now;
            _wgmSvc.GetServerTimeAsync();
            _wgmSvc.GetAllUserOptionsAsync(_username, _password, _networkName, false);
            _wgmSvc.GetAllFontsAsync();
            _progressPanel.Visibility = Visibility.Visible;
            
        }

        internal void SendMessage(string receiverUsername, string message, ChatControl sender)
        {
            WGM_Svc.WGMMessage wgmMessage = new WGM_Silverlight.WGM_Svc.WGMMessage();
            wgmMessage.Message = message;
            wgmMessage.SenderLogon = _username;
            wgmMessage.ReceiverLogon = receiverUsername;
            wgmMessage.MessageType = sender.IsMainWindow ? WGM_Svc.MessageTypes.GLOBAL_MESSAGE : WGM_Silverlight.WGM_Svc.MessageTypes.PRIVATE_MESSAGE;
            wgmMessage.TimeStamp = DateTime.Now;

            _wgmSvc.SendMessageAsync(wgmMessage, _username, _password, _networkName, new object[] { wgmMessage, sender });
        }

        private void OnRetry(object sender, MouseButtonEventArgs e)
        {

        }

        private void OnCancel(object sender, MouseButtonEventArgs e)
        {

        }

        private void OnOptionsClick(object sender, MouseButtonEventArgs e)
        {
            _optionsView.Init(MyContactInfo.ContactUserOptions, this, _allFonts, _myContactImageBrush.ImageSource);
            if (_optionsTab.IsSelected == true)
                _chatTab.IsSelected = true;
            else
                _optionsTab.IsSelected = true;
        }

        internal void SetUserOptions(WGM_Silverlight.WGM_Svc.UserOptions myNewOptions)
        {
            myNewOptions.UserName = _username;
            _wgmSvc.SetUserOptionsAsync(_username, _password, _networkName, myNewOptions);
            _chatTab.IsSelected = true;
        }

        internal void CancelChangeOptions()
        {
            _optionsView.Init(MyContactInfo.ContactUserOptions, this, _allFonts, _myContactImageBrush.ImageSource);

            _chatTab.IsSelected = true;
        }

        private void OnViewChanged(object sender, SelectionChangedEventArgs e)
        {
            if (_optionsBtn != null)
            {
                if (_optionsTab != null && _optionsTab.IsSelected)
                    _optionsBtn.Text = "Return to chat";
                else
                    _optionsBtn.Text = "Options";
            }
        }

        internal void SetUserImage(byte[] imageArr)
        {
            _wgmSvc.SetUserImageAsync(_username, _password, _networkName, imageArr);
        }

        private void OnGoToNextUnread(object sender, MouseButtonEventArgs e)
        {
            foreach (ContactInfo ci in _contactList.Children)
            {
                if (ci.HasNewMessage)
                {
                    ci_MouseLeftButtonDown(ci, null);
                    return;
                }
            }
        }
    }
}
